package StuDemo.LinkedList;

import com.sun.javafx.tk.TKPulseListener;

import java.security.PublicKey;

/**
 * @Author: Dijah
 * @Date: 2022/12/15 10:55
 * @Desc: 反转一个链表的一部分
 */
public class code3_reverseBetween {

   public ListNode reverseBetween(ListNode head,int m ,int n){

        if(m == 1){
            //反转整链表
            return reverseN(head,n);
        }
        head.next = reverseBetween(head.next,m-1,n-1);
        return head;


    }

    /**
     * 反转前n个链表
     * @param head
     * @param n
     * @return
     */
    public ListNode reverseN(ListNode head,int n){
        ListNode successor = null;
        if(n == 1){
            successor = head.next;
            return head;
        }
        head.next.next = head;
        head.next = successor;
        return head;
    }
}
